<template>
  <div v-loading="loading">
    <el-table
      :data="sumList"
      style="width: 100%"
      :height="tableHeight"
      empty-text="查询条件下没有找到记录"
    >
      <el-table-column prop="period" label="期间" width="120" />
      <el-table-column prop="coursename" v-if="!hideColumn" label="课程" width="150" />
      <el-table-column prop="teachername" label="教师" width="120" />
      <el-table-column prop="cnt" label="课节数" width="120" />
    </el-table>
    <div v-loading="loading" class="my_opt_btn_pannel">
      <div style="text-align: left"></div>
      <div style="text-align: right; padding-right: 20px">
        <el-button @click="searchVisable = true">搜索</el-button>
        <el-button @click="doSearch()">刷新</el-button>
      </div>
    </div>
    <el-dialog v-model="searchVisable" title="搜索条件" width="600">
      <el-form :model="search">
        <el-form-item label="月份">
          <el-date-picker clearable style="width: 300px" v-model="search.s_Date" type="month" />
        </el-form-item>
        <el-form-item label="分组">
          <el-select style="width: 300px" v-model="search.s_GroupBy">
            <el-option value="教师+课程" />
            <el-option value="按教师" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="Search" @click="doSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { SumSchedule } from '@/api/rpt'
import { computed, onMounted, reactive, ref } from 'vue'
import { useWindowSize } from '@vueuse/core'
import { dateFormat } from '@/utils/stringUtils'
const hideColumn = ref(false)
const searchVisable = ref(false)
const winSize = useWindowSize()
const loading = ref(false)
const sumList = ref<any>()
const tableHeight = computed(() => winSize.height.value - 60 - 8 - 42) //60是MENU,8是PADDING-TOP，42是按钮区
const search = reactive({
  s_Date: new Date(),
  s_GroupBy: '教师+课程',
})
onMounted(async () => {
  await doSearch()
})
const doSearch = async () => {
  searchVisable.value = false
  try {
    loading.value = true
    const r = await SumSchedule(dateFormat(search.s_Date).substring(0, 7))
    if (search.s_GroupBy === '教师+课程') sumList.value = r.Data
    else {
      const s: { period: string; teachername: string; cnt: number }[] = []
      r.Data.forEach((t) => {
        const s1 = s.find((u) => u.period === t.period && u.teachername === t.teachername)
        if (!s1) s.push({ period: t.period, teachername: t.teachername, cnt: parseInt(t.cnt) })
        else s1.cnt += parseInt(t.cnt)
      })
      sumList.value = s
    }
    hideColumn.value = search.s_GroupBy !== '教师+课程'
  } finally {
    loading.value = false
  }
}
</script>
